{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d8693c8e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-10-07T14:42:18.770904Z",
     "start_time": "2022-10-07T14:42:10.515802Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import lightgbm as lgb\n",
    "from datetime import datetime,timedelta\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import roc_auc_score,log_loss\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "import warnings\n",
    "from catboost import CatBoostClassifier\n",
    "from xgboost import XGBClassifier\n",
    "from lightgbm import LGBMClassifier\n",
    "warnings.filterwarnings('ignore')\n",
    "pd.set_option('display.max_columns',100)\n",
    "pd.set_option('display.max_rows',100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0703942b-5755-4fb5-ac57-4b85ca007472",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1_数据读取_B榜\t\t   B榜结果.csv\t     test.csv\n",
      "2_模型训练_B榜_lgb.ipynb   fea\t\t     train.csv\n",
      "3_模型训练_B榜_cbt.ipynb   pred_89129_61618  说明文档_B榜.pdf\n",
      "4_模型训练_B榜_xgb.ipynb   pred_89323_61785\n",
      "5_模型融合_B榜_vote.ipynb  pred_89479_61914\n"
     ]
    }
   ],
   "source": [
    "!ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f00ad08f-ba98-476f-b9e8-6632efb0f4e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train = pd.read_csv('train.csv')\n",
    "X_test = pd.read_csv('test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0d00fa7a-a013-48ad-81b3-f74929ac35ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "y = X_train['Flag']\n",
    "X_train = X_train.drop(['CUST_NO', 'Flag'],axis=1)\n",
    "X_test = X_test.drop(['CUST_NO'],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9c614ccb-8d43-40b2-a46a-be8c916a3f1b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['NTRL_CUST_SEX_CD', 'NTRL_CUST_AGE', 'NTRL_RANK_CD', 'AST_DAY_FA_BAL',\n",
       "       'AST_MAVER_FA_BAL', 'AST_SAVER_FA_BAL', 'AST_YAVER_FA_BAL',\n",
       "       'AST_DAY_AUM_BAL', 'AST_MAVER_AUM_BAL', 'AST_SAVER_AUM_BAL',\n",
       "       ...\n",
       "       'IBTF_YEAR_TR_CNT_IN', 'TPAY_MOTH_TR_AMT', 'TPAY_SEAN_TR_AMT',\n",
       "       'TPAY_MOTH_NET_TR_AMT', 'TPAY_SEAN_NET_TR_AMT', 'TPAY_MOTH_TR_CNT',\n",
       "       'TPAY_SEAN_TR_CNT', 'IBTF_TPAY_MOTH_TR_AMT',\n",
       "       'IBTF_TPAY_MOTH_NET_TR_AMT', 'IBTF_TPAY_MOTH_TR_CNT'],\n",
       "      dtype='object', length=368)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8321d4de",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-10-07T14:42:27.645043Z",
     "start_time": "2022-10-07T14:42:27.616989Z"
    }
   },
   "outputs": [],
   "source": [
    "# Some useful parameters which will come in handy later on\n",
    "ntrain = X_train.shape[0]\n",
    "ntest = X_test.shape[0]\n",
    "SEED = 2019 # for reproducibility\n",
    "NFOLDS = 5 # set folds for out-of-fold prediction\n",
    "kf = StratifiedKFold(n_splits= NFOLDS, random_state=SEED,shuffle=True)\n",
    "\n",
    "# Class to extend the Sklearn classifier\n",
    "class SklearnHelper(object):\n",
    "    def __init__(self, clf, seed=0, params=None):\n",
    "        params['random_state'] = seed\n",
    "        self.clf = clf(n_jobs=10,**params)\n",
    "\n",
    "    def train(self, x_train, y_train, x_valid, y_valid):\n",
    "        self.clf.fit(x_train, y_train,eval_set=[(x_valid, y_valid)],\n",
    "                     early_stopping_rounds=500,verbose=200)\n",
    "\n",
    "    def predict(self, x):\n",
    "        return self.clf.predict_proba(x)[:,1]\n",
    "    \n",
    "    def fit(self,x_train,y_train, x_valid,y_valid):\n",
    "        return self.clf.fit(x,y,eval_set=[(x_valid, y_valid)],early_stopping_rounds=500)\n",
    "    \n",
    "    def feature_importances(self,x,y):\n",
    "        print(self.clf.fit(x,y).feature_importances_)\n",
    "    \n",
    "# Class to extend XGboost classifer\n",
    "\n",
    "\n",
    "\n",
    "def get_oof(clf, x_train, y_train, x_test):\n",
    "    oof_train = np.zeros((ntrain,))\n",
    "    oof_test = np.zeros((ntest,))\n",
    "    oof_test_skf = np.empty((NFOLDS, ntest))\n",
    "\n",
    "    for i, (train_index, test_index) in enumerate(kf.split(x_train,y)):\n",
    "        x_tr = x_train[train_index]\n",
    "        y_tr = y_train[train_index]\n",
    "        x_te = x_train[test_index]\n",
    "        y_te = y_train[test_index]\n",
    "        \n",
    "        clf.train(x_tr, y_tr,x_te,y_te)\n",
    "\n",
    "        oof_train[test_index] = clf.predict(x_te)\n",
    "        oof_test_skf[i, :] = clf.predict(x_test)\n",
    "\n",
    "    oof_test[:] = oof_test_skf.mean(axis=0)\n",
    "    return oof_train.reshape(-1, 1), oof_test.reshape(-1, 1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1c87faa1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-10-07T14:42:27.676741Z",
     "start_time": "2022-10-07T14:42:27.647720Z"
    }
   },
   "outputs": [],
   "source": [
    "params_xgb = {\n",
    "    'n_estimators':20000,\n",
    "         'objective': 'binary:logistic',\n",
    "         'max_depth': 7,\n",
    "         'learning_rate': 0.03,\n",
    "         \"booster\": \"gbtree\",\n",
    "         \"eval_metric\": 'auc',\n",
    "         \"gamma\": 0.1,\n",
    "        'colsample_bytree':0.8,\n",
    "        \n",
    "      #  'tree_method':'gpu_hist',\n",
    "        'seed':1024}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e103709a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-10-06T01:55:38.082013Z",
     "start_time": "2022-10-06T01:52:35.963918Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0]\tvalidation_0-auc:0.86965\n",
      "[200]\tvalidation_0-auc:0.88864\n",
      "[400]\tvalidation_0-auc:0.88742\n",
      "[600]\tvalidation_0-auc:0.88567\n",
      "[621]\tvalidation_0-auc:0.88563\n",
      "[0]\tvalidation_0-auc:0.87506\n",
      "[200]\tvalidation_0-auc:0.89855\n",
      "[400]\tvalidation_0-auc:0.89818\n"
     ]
    }
   ],
   "source": [
    "clf_xgb = SklearnHelper(clf=XGBClassifier, seed=1024,params = params_xgb)\n",
    "oof_xgb, test_xgb = get_oof(clf_xgb, X_train.values,y,X_test.values)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4959c877",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-10-06T01:55:38.113176Z",
     "start_time": "2022-10-06T01:55:38.084079Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.8932378379269832\n"
     ]
    }
   ],
   "source": [
    "auc = roc_auc_score(y,oof_xgb)\n",
    "print(roc_auc_score(y,oof_xgb))  # 0.8932378379269832"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c704bb09-ef79-44c7-9b8b-3407b7958233",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "670bd9cf-2f2a-4851-8770-08ba91db15fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "oof_stack = oof_xgb\n",
    "predictions_stack = test_xgb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a32c921a-82b9-45cc-bfe4-69da03dad3fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "#np.min(oof_stack),np.max(oof_cbt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3396a851-8d18-4e86-a296-7eec6b417482",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import f1_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ece1960c-11be-4858-adbb-af71b4880148",
   "metadata": {},
   "outputs": [],
   "source": [
    "thresholds = np.arange(0,1,0.01)\n",
    "f1_scores = []\n",
    "for x in thresholds:\n",
    "    pred_label = np.where(oof_stack>x,1,0)\n",
    "    score = f1_score(y,pred_label)\n",
    "    f1_scores.append(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "224aef7a-d929-4066-a784-0e87849e6815",
   "metadata": {},
   "outputs": [],
   "source": [
    "best_score = np.max(f1_scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "bcba8fb3-9d0f-4134-a06c-3544c5d6d866",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6178518674825911\n"
     ]
    }
   ],
   "source": [
    "print(np.max(f1_scores))  # 0.6178518674825911"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "3b396fe4-d76c-4dff-9117-228d9e95a61e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([3677.,  459.,  239.,  142.,  102.,   83.,   89.,   71.,   92.,\n",
       "          70.]),\n",
       " array([0.00748214, 0.10395238, 0.20042263, 0.29689287, 0.39336312,\n",
       "        0.48983336, 0.58630361, 0.68277385, 0.7792441 , 0.87571434,\n",
       "        0.97218459]),\n",
       " <BarContainer object of 10 artists>)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KG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1ar/LXfEsX8M+FySf8HgQu8vniUneiT5AoMP9IvaNYzbgXMBquq3GFzTuA6YBV4DblrwmGfJn40kaUSrdapHkrREBr8kdcbgl6TOGPyS1BmDX5I6Y/BLUmcMfknqzP8DSth1UdXlD2oAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.hist(predictions_stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "cffb24ca-c1b0-42cd-ba00-4a778b13f1cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "best_threshold = thresholds[np.argmax(f1_scores)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "838adff0-f73c-4dec-9611-c066b0eedf61",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.31\n"
     ]
    }
   ],
   "source": [
    "print(best_threshold)  # 0.31"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "6621f610-3981-47e8-a9cc-db08926f54d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "pred = np.where(predictions_stack>best_threshold,1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "743d27ea-f198-4796-b92a-14c5b6db92e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1498802e-86e0-4fe5-8e30-2a0838ba234a",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_path = '../../contest/train'\n",
    "stage_path = '../../contest/B榜'\n",
    "stage = 'B'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e1d2aa67-5d19-4433-968b-9fed04099638",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_test = pd.read_csv(os.path.join(stage_path,f'DZ_TARGET_TESTB.csv'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b89b57fa-a1e6-4d25-a145-4d2498541c86",
   "metadata": {},
   "outputs": [],
   "source": [
    "pred = pred.reshape(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "9235ac4e-99aa-42d1-92e4-4ece05fd4203",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = pd.DataFrame({'CUST_NO':df_test.cust_no,'Flag':pred})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1aaf667f-12f4-460d-94fc-a5ab76b3291f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    4398\n",
       "1     626\n",
       "Name: Flag, dtype: int64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.Flag.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "53b3517d-6dc0-45af-99e7-07d7cf8cd515",
   "metadata": {},
   "outputs": [],
   "source": [
    "result.to_csv('pred_{0}_{1}'.format(str(auc)[2:7], str(best_score)[2:7]),header=False,index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d623d6c-32a8-4a92-ad24-963edd59a96a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
